package com.bjp.recruitersprofile.action;

import java.util.Date;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletConfig;
import javax.portlet.PortletSession;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

import com.bjp.recruitersprofile.model.Profile;
import com.bjp.recruitersprofile.model.impl.ProfileImpl;
import com.bjp.recruitersprofile.service.ProfileLocalServiceUtil;
import com.bjp.recruitersprofile.service.ProfileServiceUtil;
import com.liferay.portal.kernel.util.Constants;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.model.User;
import com.liferay.portal.struts.PortletAction;
import com.liferay.portal.util.PortalUtil;


public class RecruitersProfileAction extends PortletAction {
	private static final Log log = LogFactory.getLog(RecruitersProfileAction.class);

	public void processAction(ActionMapping mapping, ActionForm form,
			PortletConfig config, ActionRequest req, ActionResponse res)
			throws Exception {

		String cmd = ParamUtil.getString(req, "cmd");

		if (cmd.equals(Constants.ADD)) {
			add(mapping, form, config, req, res);
		}
		else if (cmd.equals(Constants.UPDATE)) {
			add(mapping, form, config, req, res);
			//setForward(req, "bjp.seekersprofile.add");
		}
	}

	private void add(ActionMapping mapping, ActionForm form,
			PortletConfig config, ActionRequest req, ActionResponse res)
			throws Exception {

		log.info("Inside SeekersProfileAction file - Method: loadpage");

		HttpServletRequest httpReq = PortalUtil.getHttpServletRequest(req);
		
		long profileId = ParamUtil.getLong(req, "profileId");
		Profile profile = null;
		boolean update = false;
		
		Long userId = PortalUtil.getUserId(req);
		PortletSession ps = req.getPortletSession();

		String inchargeName = ParamUtil.getString(req, "inchargeName");
		String mobile = ParamUtil.getString(req, "mobile");
		String address = ParamUtil.getString(req, "address");
		String emailAddress = ParamUtil.getString(req, "emailAddress");
				
		if (profileId > 0l) {
			profile = ProfileLocalServiceUtil.getProfile(profileId);
			update = true;
		} else {
			profile = new ProfileImpl();
		}
		profile.setInchargeName(inchargeName);
		profile.setDateAdded(new Date());
		profile.setMobile(mobile);
		profile.setAddress(address);
		profile.setEmailAddress(emailAddress);
			
		profile.setDateAdded(new Date());
		profile.setAddedBy(userId);
		try {
			if (update) {
				ProfileLocalServiceUtil.getService().updateProfile(profile);
			} else {
				ProfileLocalServiceUtil.getService().addProfile(profile);
			}
		} catch(Exception e) {
			setForward(req, "bjp.recruitersprofile.add");
		}
		profile = (Profile) ProfileLocalServiceUtil.getProfile(profile.getProfileId());
		req.setAttribute("PROFILE", profile);
		
		setForward(req, "bjp.recruitersprofile.view");
	}
	
}
